@extends('admin/common')

@section('content')
    <article class="page-container">
        <form action="" method="post" class="form form-horizontal" id="myform" onsubmit="return sub();">
            <div class="row cl">
                <label class="form-label col-xs-4 col-sm-3">权限列表：</label>
                <div class="formControls col-xs-8 col-sm-9">
                    <dl class="permission-list">
                        <dt>
                            <label>
                                <input type="checkbox" value="" name="all-check" id="all-check">
                                全选</label>
                        </dt>
                        @if(isset($all_permission))
                        @foreach($all_permission as $k => $v)
                        <dd>
                            <dl class="cl permission-list2">
                                <dt>
                                    <label class="">
                                        <input type="checkbox" value="{{ $v['id'] }}" name="p-perm" >
                                        {{ $v['name'] }}</label>
                                </dt>
                                @if(isset($v['child']) && is_array($v['child']))
                                <dd>
                                    @foreach($v['child'] as $c_k => $c_v)
                                    <label class="">
                                        <input type="checkbox" value="{{ $c_v['id'] }}" name="perm" >
                                        {{ $c_v['name'] }}</label>
                                    @endforeach
                                </dd>
                                @endif
                            </dl>
                        </dd>
                        @endforeach
                        @endif
                    </dl>
                </div>
            </div>
            <div class="row cl">
                <div class="col-xs-8 col-sm-9 col-xs-offset-4 col-sm-offset-3">
                    <button type="submit" class="btn btn-success radius" id="admin-role-save" name="admin-role-save"><i class="icon-ok"></i> 确定</button>
                </div>
            </div>
        </form>
    </article>
    @endsection

@section('script')
    <script type="text/javascript">
        $(function(){
           //全选按钮
            $('.permission-list').on('click','#all-check',function(){
                if($(this).is(':checked')) {
                    $('input[name="p-perm"]').prop('checked',true);
                    $('input[name="perm"]').prop('checked',true);
                }else {
                    $('input[name="p-perm"]').prop('checked',false);
                    $('input[name="perm"]').prop('checked',false);
                }
            });
        });

        //已拥有的角色
        var ids_s =  "{{ (isset($ids) && !is_array($ids)) ? $ids : '' }}";
        if(ids_s != ''){
            var ids = ids_s.split(',');
            var len = ids.length;
            for(var i=0; i< len; i++){
                $('input[type="checkbox"]').each(function(index, item){
                    console.log($(item).val());
                    if($(item).val() == ids[i]){
                        $(item).prop('checked',true);
                    }
                });
            }
        }

        function sub(){
            var p_ids = [];
            $('input[name="p-perm"]:checked').each(function(index, item){
                p_ids.push($(item).val());
            });

            $('input[name="perm"]:checked').each(function(index, item){
                p_ids.push($(item).val());
            });

            $.ajax({
                url : "{{ URL::to('admin/role/rolePermissionPost') }}",
                type : 'POST',
                data : {
                    p_ids : p_ids.join(),
                    role_id : "{{ $role_id }}"
                },
                dataType : 'JSON',
                success : function(ret){
                    if(ret.code == 1){
                        layer.msg(ret.msg, {icon:1,time:1000},function(){
                            var index = parent.layer.getFrameIndex(window.name);
                            parent.layer.close(index);
                        });
                    }else{
                        layer.msg(ret.msg, {icon:1,time:1000});
                    }
                }
            });
            return false;
        }
    </script>
    @endsection